package com.jee.ssm.modules.hikDeviceBerth.controller;

import com.jee.ssm.model.HikDevice;
import com.jee.ssm.model.HikDeviceBerth;
import com.jee.ssm.model.PosUser;
import com.jee.ssm.model.PosuserBerth;
import com.jee.ssm.modules.hikDevice.services.HikDeviceService;
import com.jee.ssm.modules.hikDeviceBerth.services.HikDeviceBerthService;
import com.jee.ssm.common.log.AdminControllerLog;
import com.jee.ssm.modules.ssm.controller.AdminBaseController;
import com.jee.ssm.model.json.Tip;
import com.jee.ssm.model.param.ParamMap;
import com.jee.ssm.common.utils.UUIDFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
* 海康设备-车位管理 Controller
* @author GaoXiang
* @version 1.0
*/
@Controller
@RequestMapping("/hikDeviceBerth")
public class HikDeviceBerthController extends AdminBaseController<HikDeviceBerth> {


    /**
     * 进入海康设备-车位添加页面 携带一个生成的id --> longId
     * @param model 返回的实体容器
     * @return 添加页面
     */
    @RequestMapping(value="/add")
    @RequiresPermissions("hikDeviceBerth:add")
    public String add(ModelMap model){

        model.put("longId", UUIDFactory.getStringId());
        return "manager/hikDeviceBerth/add";
    }


    /**
     * 进入海康设备-车位编辑页面
     * @param model 返回海康设备-车位的容器
     * @param id 海康设备-车位id
     * @return 编辑页面
     */
    @RequestMapping(value="/edit")
    @RequiresPermissions("hikDeviceBerth:edit")
    public String edit(ModelMap model, @RequestParam String id) {

        model.put("data",hikDeviceBerthService.selectById(id));
        return "manager/hikDeviceBerth/edit";
    }


    /**
     * 海康设备-车位添加
     * @param hikDeviceBerth 带id的海康设备-车位对象
     * @return 成功状态
     */
    @RequestMapping(value="/save")
    @ResponseBody
    @RequiresPermissions("hikDeviceBerth:add")
    @AdminControllerLog(description = "添加海康设备-车位" )
    public Tip save(HikDeviceBerth hikDeviceBerth)  {

        try {
            hikDeviceBerthService.insert(hikDeviceBerth);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"添加失败！");
        }

    }


    /**
     * 根据 id 修改海康设备-车位
     * @param hikDeviceBerth 带id的海康设备-车位对象
     * @return 成功状态
     */
    @RequestMapping(value="/update")
    @ResponseBody
    @RequiresPermissions("hikDeviceBerth:edit")
    @AdminControllerLog(description = "修改海康设备-车位" )
    public Tip update(HikDeviceBerth hikDeviceBerth) {

        try {
            hikDeviceBerthService.updateById(hikDeviceBerth);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"修改失败！");
        }

    }


    /**
     * 根据 id 删除海康设备-车位
     * @param id 海康设备-车位id
     * @return 成功状态
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    @RequiresPermissions("hikDeviceBerth:delete")
    @AdminControllerLog(description = "删除海康设备-车位" )
    public Tip delete(@RequestParam String id) {

        try {
            hikDeviceBerthService.deleteById(id);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"删除失败！");
        }

    }


    /**
     * 根据 id 列表批量删除
     * @param ids 海康设备-车位id List
     * @return 成功状态
     */
    @RequestMapping(value="/deleteByIds")
    @ResponseBody
    @RequiresPermissions("hikDeviceBerth:delete")
    @AdminControllerLog(description = "批量删除海康设备-车位" )
    public Tip deleteByIds(@RequestParam("ids") List<String> ids) {

        try {
            hikDeviceBerthService.deleteByIds(ids);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"批量删除失败！");
        }

    }


    /**
     * 根据 id 查找海康设备-车位
     * @param id 海康设备-车位id
     * @return 海康设备-车位对象 json
     */
    @RequestMapping(value="/findJson")
    @ResponseBody
    @RequiresPermissions("hikDeviceBerth:list")
    public HikDeviceBerth find(@RequestParam String id) {

        return hikDeviceBerthService.selectById(id);
    }


    /**
     * 获取海康设备-车位列表 获取全部 不分页
     * @param request 请求参数
     * @return 海康设备-车位列表页面
     */
    @RequestMapping(value="/all")
    @RequiresPermissions("hikDeviceBerth:list")
    public String all(HttpServletRequest request,ModelMap modelMap) {

        return list(request,modelMap,1,0);
    }


    /**
     * 获取海康设备-车位列表 分页
     * @param request 请求参数
     * @param page 第几页
     * @param size 每页大小
     * @return 海康设备-车位列表页面
     */
    @RequestMapping(value="/list")
    @RequiresPermissions("hikDeviceBerth:list")
    public String list(HttpServletRequest request,ModelMap modelMap,Integer page,Integer size) {

        modelMap.put("pageInfo",hikDeviceBerthService.list(new ParamMap(request),page,size));
        modelMap.putAll(new ParamMap(request));
        return "manager/hikDeviceBerth/list";
    }


    /**
     * 描述：更新绑定信息
     * 创建时间：2020/7/20  YanZengBao
     * 修改时间：2020/7/20  YanZengBao 加注释
     * 修改时间：
     */
    @RequestMapping("/updateHikDeviceBerth")
    @ResponseBody
    public Tip updateHikDeviceBerth(@RequestParam("berthIds") List<String> berthIds, String id) {

        try {
            HikDevice hikDevice = hikDeviceService.selectById(id);
            if (hikDevice == null) return new Tip(2, "未获取到海康设备信息");
            //根据设备ID删除绑定的关联数据
            hikDeviceBerthService.deleteByHikId(id);

            for (String berthId : berthIds) { //车位的ID
                if (berthId != null && !"".equals(berthId)) {
                    HikDeviceBerth hikDeviceBerth = new HikDeviceBerth(UUIDFactory.getStringId(), id, hikDevice.getHikNumber(), hikDevice.getHikName(), berthId, "", new Date());
                    hikDeviceBerthService.insert(hikDeviceBerth);
                }
            }
            return new Tip("设置成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(2, "设置失败");
        }
    }


    //---------------------------- property -------------------------------

    @Resource
    private HikDeviceBerthService hikDeviceBerthService;
    @Resource
    private HikDeviceService hikDeviceService;

}
